package com.bygf.curve.utils;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author sunyupei
 */
public class Utils {

    /**
     * 弹出提示信息对话框
     *
     * @param component 父组件
     * @param strMsg 要显示的消息正文
     */
    public static void ShowInformation(Component component, String strMsg) {
        Object[] options = {"确定"};
        JOptionPane.showOptionDialog(component, strMsg, "提示", JOptionPane.DEFAULT_OPTION,
                JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
    }

    /**
     * 根据字符串及字符串所用的字体，计算字符串的像素宽度
     *
     * @param str
     * @param font
     * @return
     */
    public static int getStringWidth(String str, Font font) {
        Font fontTemp;
        if (font == null) {
            fontTemp = new Font("宋体", Font.PLAIN, 12);
        } else {
            fontTemp = font;
        }
        JLabel labelTemp = new JLabel(str);
        labelTemp.setFont(fontTemp);
        FontMetrics fm = labelTemp.getFontMetrics(fontTemp);
        return fm.stringWidth(str) + 2;
    }

    /**
     * 保存文件
     *
     * @param fileBytes
     * @param fileName
     * @return
     */
    public static String saveFile(byte[] fileBytes, String fileName) {
        try {
            byte[] imgBufTemp = new byte[1024];
            FileOutputStream fos;
            try (InputStream stream = new ByteArrayInputStream(fileBytes)) {
                File file = new File(fileName);
                fos = new FileOutputStream(file);
                int length;
                while ((length = stream.read(imgBufTemp)) != -1) {
                    fos.write(imgBufTemp, 0, length);
                }
            }
            fos.close();
            return fileName;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将Image转换成byte[]
     *
     * @param img
     * @return
     */
    public static byte[] Image2Byte(Image img) {
        BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
        bi.createGraphics().drawImage(img, 0, 0, null);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            ImageIO.write(bi, "jpg", stream);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        byte[] imageByte = stream.toByteArray();
        return imageByte;
    }

    /**
     * 进行曲线界面截图
     *
     * @param panel 待截图的Panel
     * @return
     */
    public static BufferedImage printScreen(JPanel panel) {
        BufferedImage image = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        panel.paint(g);
        g.dispose();
        return image;
    }
    
    /**
     * 调整JDialog居于屏幕正中
     *
     * @param dialog 要居中的窗体
     */
    public static void CenterDialog(JDialog dialog) {
        // 获得屏幕的大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension dialogSize = dialog.getSize();
        // 调整窗口大小，使其不超过屏幕
        if (dialogSize.height > screenSize.height) {
            dialogSize.height = screenSize.height;
        }
        if (dialogSize.width > screenSize.width) {
            dialogSize.width = screenSize.width;
        }
        // 设置窗口居于屏幕正中
        dialog.setLocation((screenSize.width - dialogSize.width) / 2,
                (screenSize.height - dialogSize.height) / 2);
        dialog.setVisible(true);
    }
}
